import 'package:flutter/material.dart';
import 'package:platform_info/platform_info.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Platform Info Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const PlatformInfoPage(title: 'Platform Info Demo'),
    );
  }
}

class PlatformInfoPage extends StatefulWidget {
  const PlatformInfoPage({super.key, required this.title});

  final String title;

  @override
  State<PlatformInfoPage> createState() => _PlatformInfoPageState();
}

class _PlatformInfoPageState extends State<PlatformInfoPage> {
  late Map<String, String> platformInfo;

  @override
  void initState() {
    super.initState();
    _initializePlatformInfo();
  }

  void _initializePlatformInfo() {
    final design = platform.when<String?>(
      vm: () => platform.when<String>(
        material: () => 'Android, Fuchsia or HarmonyOS',
        cupertino: () => 'macOS or iOS',
        orElse: () => 'Windows or Linux',
      ),
      js: () => 'Web',
    );

    final operatingSystem = switch (platform.operatingSystem) {
      const OperatingSystem.android() => 'Android',
      const OperatingSystem.fuchsia() => 'Fuchsia',
      const OperatingSystem.iOS() => 'iOS',
      const OperatingSystem.linux() => 'Linux',
      const OperatingSystem.macOS() => 'macOS',
      const OperatingSystem.windows() => 'Windows',
      const OperatingSystem.ohos() => 'HarmonyOS',
      const OperatingSystem.unknown() || _ => 'Unknown',
    };

    final buildMode = switch (platform.buildMode) {
      BuildMode$Debug _ => 'Debug',
      BuildMode$Profile _ => 'Profile',
      BuildMode$Release _ => 'Release',
    };

    final platformMessage = platform.when<String?>(
      ohos: () => 'Welcome to HarmonyOS!',
      android: () => 'Welcome to Android!',
      iOS: () => 'Welcome to iOS!',
      orElse: () => 'Welcome to ${platform.operatingSystem.name}!',
    );

    platformInfo = {
      '平台类型': platform.when<String>(
        vm: () => 'VM (Native)',
        js: () => 'JavaScript (Web)',
        orElse: () => 'Unknown',
      ) ?? 'Unknown',
      '操作系统': operatingSystem,
      '系统版本': Platform.instance.version,
      '构建模式': buildMode,
      '设计风格': design ?? 'Unknown',
      '设备类型': platform.mobile ? 'Mobile' : (platform.desktop ? 'Desktop' : 'Unknown'),
      '处理器核心数': platform.numberOfProcessors.toString(),
      '系统语言': platform.locale,
      '欢迎消息': platformMessage ?? 'Welcome!',
      '是否为鸿蒙系统': platform.ohos ? '是' : '否',
      '是否为移动设备': platform.mobile ? '是' : '否',
      '是否为Material设计': platform.material ? '是' : '否',
      '是否为Cupertino设计': platform.cupertino ? '是' : '否',
    };
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
      ),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: ListView.builder(
          itemCount: platformInfo.length,
          itemBuilder: (context, index) {
            final key = platformInfo.keys.elementAt(index);
            final value = platformInfo[key]!;
            return Card(
              child: ListTile(
                title: Text(
                  key,
                  style: const TextStyle(fontWeight: FontWeight.bold),
                ),
                subtitle: Text(
                  value,
                  style: TextStyle(
                    color: _getValueColor(key, value),
                    fontSize: 16,
                  ),
                ),
                leading: Icon(
                  _getIconForKey(key),
                  color: Theme.of(context).primaryColor,
                ),
              ),
            );
          },
        ),
      ),
    );
  }

  Color _getValueColor(String key, String value) {
    if (key == '操作系统') {
      switch (value) {
        case 'HarmonyOS':
          return Colors.orange;
        case 'Android':
          return Colors.green;
        case 'iOS':
          return Colors.blue;
        default:
          return Colors.grey;
      }
    }
    if (key == '构建模式') {
      switch (value) {
        case 'Debug':
          return Colors.red;
        case 'Profile':
          return Colors.orange;
        case 'Release':
          return Colors.green;
        default:
          return Colors.grey;
      }
    }
    return Colors.black87;
  }

  IconData _getIconForKey(String key) {
    switch (key) {
      case '平台类型':
        return Icons.computer;
      case '操作系统':
        return Icons.phone_android;
      case '系统版本':
        return Icons.info;
      case '构建模式':
        return Icons.build;
      case '设计风格':
        return Icons.design_services;
      case '设备类型':
        return Icons.devices;
      case '处理器核心数':
        return Icons.memory;
      case '系统语言':
        return Icons.language;
      case '欢迎消息':
        return Icons.waving_hand;
      case '是否为鸿蒙系统':
        return Icons.smartphone;
      case '是否为移动设备':
        return Icons.mobile_friendly;
      case '是否为Material设计':
        return Icons.android;
      case '是否为Cupertino设计':
        return Icons.apple;
      default:
        return Icons.info;
    }
  }
}
